<%@ page import="service.serviceimpl.ServiceUserImpl" %><%--

--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //定义全局的op来判断curd,对应操作
    String op = request.getParameter("op");//先拿标志
    if (op != null) {//有标志，可以判断curd
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        if (op.equals("delete")) {//做删除
            //拿到删除id
            int id= Integer.parseInt(request.getParameter("id"));
            //执行删除操作
            int i = serviceUser.executeUpdate("delete from userinfo where id=?", id);
            if(i>0){//删除成功
                request.getRequestDispatcher("main.jsp?delete1=t").forward(request,response);
            }else{//删除失败
                response.sendRedirect("main.jsp?delete2=t");
            }
        }
        else if(op.equals("add")){
            //着手增加
            String name = request.getParameter("name");
            String dept = request.getParameter("dept");
            int sex= Integer.parseInt(request.getParameter("sex"));
            int i=serviceUser.executeUpdate("insert into userinfo(name,sex,age,ranking,dept) values(?,?,18,'1',?) "
            ,name,sex,dept);
            if(i>0){//增加成功
                request.getRequestDispatcher("main.jsp?add1=t").forward(request,response);
            }else{//增加失败
                response.sendRedirect("main.jsp?add2=t");
            }
        }else if(op.equals("update1")){
            //拿到传入要修改的id
            int id= Integer.parseInt(request.getParameter("id"));
            response.sendRedirect("update.jsp?id="+id);
        }else if(op.equals("update2")){
            int id= Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("name");
            String dept = request.getParameter("dept");
            int sex= Integer.parseInt(request.getParameter("sex"));
            int i = serviceUser.executeUpdate("update userinfo set name=?,sex=?,dept=? where id=?", name, sex, dept, id);
            if(i>0){//修改成功
                request.getRequestDispatcher("main.jsp?update1=t").forward(request,response);
            }else{//修改失败
                response.sendRedirect("main.jsp?update2=t");
            }

        }

    } else {
        response.sendRedirect("main.jsp?feifa=t");
    }


%>

</body>
</html>
